package me.toalec.RankUp;

import me.toalec.RankUp.Permissions.PermissionsManager;
import me.toalec.RankUp.util.Utility;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class CommandTrigger implements CommandExecutor{

	CommandTrigger(RankUp instance, ConfigManager config, PermissionsManager pm){
		this.instance = instance;
		this.config = config;
		this.pm = pm;
	}

	@Override
	public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
		if(sender instanceof Player){
			Player commandUser = (Player)sender;
			String startingGroup;
			String endingGroup;
			String packageName;
				
			if(args.length == 1){
				if(config.getBoolean("default.rankup_command_enabled")){
					
					startingGroup = config.getString("default.startingGroup");
					endingGroup = config.getString("default.endingGroup");
					packageName = "default";
						
				}else{
					Utility.messagePlayer(commandUser, "The /rankup command is disabled.");
					return true;
				}
			}else if(args.length == 2){
				if(config.getAllPackageNames().contains(args[1]) && config.getBoolean(args[1] + ".rankup_command_enabled")){
						
					packageName = args[1];
					startingGroup = config.getString(args[1] + ".startingGroup");
					endingGroup = config.getString(args[1] + ".endingGroup");
					
				}else{
					Utility.messagePlayer(commandUser, "The /rankup command is disabled, or an incorrect package name was entered.");
					return true;
				}
			}else
				return false;
			
			if( pm.hasPermission(commandUser, "rankup.rankup.*") || pm.hasPermission(commandUser, "rankup.rankup." + packageName)){
				if( pm.isValidGroup(startingGroup, commandUser) && pm.isValidGroup(endingGroup, commandUser) ){
					if( pm.isValidPlayer(args[0], commandUser) && pm.getGroupName(args[0], commandUser).contains(startingGroup) 
							&& !(pm.getGroupName(args[0], commandUser).contains(endingGroup))){
								
						pm.removeGroup(args[0], commandUser, startingGroup);
						pm.addGroup(args[0], commandUser, endingGroup);
						Utility.messagePlayer(commandUser, args[0] + "'s rank has been successfully changed.");
								
						Player player = instance.getServer().getPlayer(args[0]);
						if(player != null && player.isOnline()){
							Utility.messagePlayer(player, "Your rank has successfully changed to " + endingGroup);
						}	
								
					}else
						Utility.messagePlayer(commandUser, "Target <player> is not in the correct Rank(Possibly because they have yet to log-in).");
				}else{
					Utility.messagePlayer(commandUser, "There is an issue with config.yml. Ask those who can, to check it.");
				}
			}else
				Utility.messagePlayer(commandUser, "You don't have permission to use this command.");
		}else
			Utility.printConsole("This command can only be used from in-game.");	
		
		return true;
	}
	
	private RankUp instance;
	private ConfigManager config;
	private PermissionsManager pm;
}
